﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
    
    partial class main_form : Form
    {

        public main_form()
        {
            InitializeComponent();
            
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            // TODO: This line of code loads data into the 'cps420_team2_pocDataSet.AddressEntry' table. You can move, or remove it, as needed.
            this.addressEntryTableAdapter.Fill(this.cps420_team2_pocDataSet.AddressEntry);

        }

        private void openToolStripMenuItem_Click(object sender, EventArgs e)
        {
            panel1.Visible = true;
        }

        private void btnGetData_Click(object sender, EventArgs e)
        {
            AddressEntryDAO dao = new AddressEntryDAO();
            dao.Connect_To_Database("Data Source=sparky;Initial Catalog=cps420_team2_poc;Persist Security Info=True;User ID=cps420team2;Password=team2");
            dataGridViewMain.DataSource = dao.LoadAllRecords();
        }

        private void btn_add_Click(object sender, EventArgs e)
        {
            AddressEntryDAO dao = new AddressEntryDAO();
            dao.Connect_To_Database("Data Source=sparky;Initial Catalog=cps420_team2_poc;Persist Security Info=True;User ID=cps420team2;Password=team2");
            dao.CreateNewRecord(new AddressEntry(txtName.Text, txtEmail.Text));
            dataGridViewMain.DataSource = dao.LoadAllRecords();
        }

        private void btn_edit_Click(object sender, EventArgs e)
        {
            AddressEntryDAO dao = new AddressEntryDAO();
            dao.Connect_To_Database("Data Source=sparky;Initial Catalog=cps420_team2_poc;Persist Security Info=True;User ID=cps420team2;Password=team2");
            dao.UpdateRecord(new AddressEntry(txtInvName.Text, txtInvEmail.Text), new AddressEntry(txtName.Text, txtEmail.Text));
            dataGridViewMain.DataSource = dao.LoadAllRecords();
        }

        private void btn_delete_Click(object sender, EventArgs e)
        {

            AddressEntryDAO dao = new AddressEntryDAO();
            dao.Connect_To_Database("Data Source=sparky;Initial Catalog=cps420_team2_poc;Persist Security Info=True;User ID=cps420team2;Password=team2");
            AddressEntry entry = new AddressEntry(txtName.Text, txtEmail.Text);
            dao.DeleteRecord(entry);
            dataGridViewMain.DataSource = dao.LoadAllRecords();
        }

        private void dataGridViewMain_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            txtName.Text = dataGridViewMain[0,e.RowIndex].Value.ToString();
            txtEmail.Text = dataGridViewMain[1, e.RowIndex].Value.ToString();
            
            txtInvName.Text = txtName.Text;
            txtInvEmail.Text = txtEmail.Text;
        }


    }
}
